import axios from "axios"
class HttpRequest {
  constructor(baseUrl) {
    this.baseUrl = baseUrl
  }
  intercepters (instance) {
    instance.interceptors.request.use(function (config) {
      let auth = {
        username: 'wcadmin',
        password: 'wcadmin'
      }
      config.headers.common['Authorization'] = auth
      // 在发送请求之前做些什么
      return config
    }, /* 对响应错误做点什么*/ function (error) {
      return Promise.reject(error)
    })
    instance.interceptors.response.use(response => {
      // let status = response.status
      return response.data
    }, error => {
      if (error.response.status) {
        switch (error.response.status) {
          case 400:
            return Promise.reject()
            break
          case 500:
            return Promise.reject('服务器错误')
          case 401:
          case 403:
            return Promise.reject()
            break
          case 404:
            // context.$Message.error("链接或文件不存在");
            return Promise.reject()
            break
          case 504:
            // context.$Message.error("服务器超时");
            return Promise.reject('服务器超时')
            break
        }
      }
      return Promise.reject(error)
    })
  }
  post(url, params = {}, contentType = '') {
    let axiosInstance = axios.create()
    this.intercepters(axiosInstance, contentType)
    return axiosInstance.post(this.baseUrl + url, params)
  }
  get(url, params = {}, contentType = '') {
    let axiosInstance = axios.create()
    this.interceptors(axiosInstance, contentType)
    return axiosInstance.get(this.baseUrl + url, {
      params
    })
  }
}
export default HttpRequest